package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Measure;
import com.hengxinguotong.zhihuichengjian.ui.quality.MeasureActivity;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/12/20.
 */

public class MeasureListAdapter extends BaseAdapter {
    private List<Measure> datas;
    private Context context;
    private int type = -1;

    public void setType(int type) {
        this.type = type;
    }

    public MeasureListAdapter(List<Measure> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(null == convertView || null == convertView.getTag()){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_measure, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        final Measure measure = datas.get(position);
        holder.structureTv.setText(measure.getProjectStructureName());
        holder.checkedCountTv.setText("总实测点数 " + measure.getTotalQty() + "");
        holder.passedCountTv.setText("总合格点数 " + measure.getPassQty() + "");
        holder.nameTv.setText(measure.getContactName());
        holder.dateTv.setText(measure.getInspectDate());
        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(type != -1){
//                    Measure measure = measureList.get(position - 1);
                    Intent intent = new Intent(context, MeasureActivity.class);
                    intent.putExtra("type", type);
                    intent.putExtra("measure", measure);
                    context.startActivity(intent);
                }else{
                    Toast.makeText(context, "页面信息有误，请重新进入当前页面", Toast.LENGTH_LONG).show();
                }
            }
        });
        return convertView;
    }

    static class ViewHolder{
        @BindView(R.id.layout)
        LinearLayout layout;
        @BindView(R.id.structure_tv)
        HXTextView structureTv;
        @BindView(R.id.checked_count_tv)
        HXTextView checkedCountTv;
        @BindView(R.id.passed_count_tv)
        HXTextView passedCountTv;
        @BindView(R.id.name_tv)
        HXTextView nameTv;
        @BindView(R.id.date_tv)
        HXTextView dateTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
